package com.ss.web.tagmark.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ss.web.tagmark.entity.Annotation;
import com.ss.web.tagmark.entity.Article;
import com.ss.web.tagmark.entity.User;
import com.ss.web.tagmark.service.ArticleService;
import com.ss.web.tagmark.service.ParameterService;
import com.ss.web.tagmark.utils.AuthContext;

/**
 * 文章相关的操作
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/articles")
public class ArticleController extends BaseController {
	@Autowired
	private ArticleService articleService;
	@Autowired
	private ParameterService parameterService;

	/**
	 * 文章列表
	 * 
	 * @param article
	 * @param request
	 * @param model
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String index(@ModelAttribute("article") Article article, HttpServletRequest request, Model model)
			throws IOException {
		Page<Article> articles = articleService.getListByPage(request.getParameterMap(), article);
		model.addAttribute("articles", articles);
		return "article/workspace";
	}

	@RequestMapping(value = "/{articleId:\\d+}/marktag", method = RequestMethod.GET)
	public String markTag(@PathVariable Long articleId, Model model) {
		model.addAttribute("article", articleService.getById(articleId));
		model.addAttribute("partOfSpeechs", parameterService.getListByType(ParameterService.PART_OF_SPEECH));
		return "article/mark_tag";
	}

	@RequestMapping(value = "/{articleId:\\d+}/marktag", method = RequestMethod.POST)
	public void markTag(@PathVariable Long articleId, Annotation ann, HttpServletResponse response) {
		try {
			articleService.markAnnotation(ann);
			renderHtml("1", response);
		} catch (Exception e) {
			renderHtml(e.toString(), response);
		}
	}

	/**
	 * 查看文件的标注结果
	 * 
	 * @param articleId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{articleId:\\d+}/showresult", method = RequestMethod.GET)
	public String showMarkResult(@PathVariable Long articleId, Model model) {
		model.addAttribute("article", articleService.getById(articleId));
		model.addAttribute("users", articleService.getUsersByArticleId(articleId));
		return "article/mark_result";
	}

	/**
	 * 显示带有当前用户标注的信息
	 * 
	 * @param articleId
	 * @param response
	 */
	@RequestMapping(value = "/{articleId:\\d+}/showwithtag", method = RequestMethod.GET)
	public void showWithTag(@PathVariable Long articleId, HttpServletResponse response) {
		renderHtml(articleService.displayArticle(articleId, AuthContext.getCurUser().getUserId()), response);
	}

	/**
	 * 获取指定 用户对文章的标注结果
	 * 
	 * @param articleId
	 * @param response
	 */
	@RequestMapping(value = "/{articleId:\\d+}/{userId:\\d+}/showwithtag", method = RequestMethod.GET)
	public void showWithUserTag(@PathVariable Long articleId, @PathVariable Long userId, HttpServletResponse response) {
		renderHtml(articleService.displayArticle(articleId, userId), response);
	}

	/**
	 * 显示带有标签的信息
	 * 
	 * @param articleId
	 * @param response
	 */
	@RequestMapping(value = "/{articleId:\\d+}/showannotations", method = RequestMethod.GET)
	public void showAnnotations(@PathVariable Long articleId, HttpServletResponse response) {
		renderHtml(articleService.getAnnotationsByArticleId(articleId, AuthContext.getCurUser().getUserId()), response);
	}

	@RequestMapping(value = "/{annId:\\d+}/annotation", method = RequestMethod.GET)
	public void showAnnotation(@PathVariable Long annId, HttpServletResponse response) {
		renderHtml(articleService.getAnnotationById(annId), response);
	}

	@RequestMapping(value = "/{annId:\\d+}/annotation/delete", method = RequestMethod.GET)
	public void deleteAnnotation(@PathVariable Long annId, HttpServletResponse response) {
		try {
			articleService.deleteAnnById(annId);
			renderHtml("", response);// 删除成功,则不输出信息
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			renderHtml(e.getMessage(), response);
		}
	}

	/**
	 * 获取标注过这个文章的用户信息
	 * 
	 * @param articleId
	 * @param ann
	 * @param response
	 */
	@RequestMapping(value = "/{articleId:\\d+}/users", method = RequestMethod.GET)
	public void users(@PathVariable Long articleId, HttpServletResponse response) {
		List<User> users = articleService.getUsersByArticleId(articleId);
		renderHtml(users, response);
	}
}
